# -*- coding: utf-8 -*-
__author__ = 'sunnychou'
__date__ = '2019/7/19 10:29'

from common.orm_helper import *
from common.excel_helper import ExcelReader
from k3_data_report.import_tb.utils import fetch_tb_unique_his_data
from  common.logger_helper import  g_wlogger
from common.utils import *

##########################################
# 2 导入k3基础表  供应商表  BEGIN
##########################################
class  GongyinShangList(Base):
    __tablename__ = "k3_gongys"  # 自定义数据库表名称
    id = Column(Integer, primary_key=True)  # 自增
    code = Column(String(20), verbose_name='代码')
    name = Column(String(100), verbose_name='名称')
    fullname = Column(String(100), verbose_name='全名')
    address = Column(String(200), verbose_name='地址')
    phone = Column(String(100), verbose_name='电话')
    fax = Column(String(100), verbose_name='传真')
    incr_rate = Column(String(10), verbose_name='增值税率(%)')
    jiesuan_bz = Column(String(10), verbose_name='结算币种')
    jiesuan_type =  Column(String(100), verbose_name='结算方式')
    sjsj = Column(String(10), verbose_name='年月')


def  gongyinshang_process(excel_name, sheet_name):
    '''
    供应商表处理过程
    :param excel_name:
    :param sheet_name:
    :return:
    '''
    year = excel_name.split('-')[1]
    month = excel_name.split('-')[2].split(".")[0]
    sjsj = f"{year}-{month}"
    ereader = ExcelReader(excel_name, sheet_name=sheet_name)
    headers = ereader.get_excel_headers()
    data_lists = ereader.get_excel_data_list()
    gys_select_sql = "select  distinct code  from k3_gongys"
    unique_set = fetch_tb_unique_his_data(gys_select_sql)
    for data_list in data_lists:
        code = data_list[0]
        if code in unique_set:
            already_info = f"供应商 代码:{code} already existed."
            g_wlogger.winfo(already_info)
            continue
        gyslist = GongyinShangList(code=data_list[0], name=data_list[1], fullname=data_list[2],
                                   address=data_list[3], phone=data_list[4],fax=data_list[5], incr_rate=data_list[6],
                                   jiesuan_bz=data_list[7], jiesuan_type=data_list[8],sjsj=sjsj
                                   )
        session.add(gyslist)
        session.commit()

    fkd_succ = f"供应商 文件：{excel_name} 处理完毕！"
    g_wlogger.winfo(fkd_succ)



def gongyinshang_process_main(excel_name, sheet_name):
    try:
        gongyinshang_process(excel_name, sheet_name)
        return True
    except Exception as e:
        g_wlogger.werror(f"gongyinshang_process_main has error:{e} with excel:{excel_name}")
        return False



##########################################
# 2 导入k3基础表  供应商表  END
##########################################


